package com.mycompany.myapp.service.impl;

import com.mycompany.myapp.domain.Commodity;
import com.mycompany.myapp.repository.CommodityRepository;
import com.mycompany.myapp.service.CommodityService;
import com.mycompany.myapp.service.MenuService;
import com.mycompany.myapp.domain.Menu;
import com.mycompany.myapp.repository.MenuRepository;
import com.mycompany.myapp.service.dto.MenuDTO;
import com.mycompany.myapp.service.mapper.MenuMapper;
import com.mycompany.myapp.web.rest.utils.ResultObj;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.LinkedList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * Service Implementation for managing {@link Menu}.
 */
@Service
@Transactional
public class MenuServiceImpl implements MenuService {

    private final Logger log = LoggerFactory.getLogger(MenuServiceImpl.class);

    private final MenuRepository menuRepository;

    private final MenuMapper menuMapper;

    @Autowired
    CommodityRepository commodityRepository;

    @Autowired
    CommodityService commodityService;

    public MenuServiceImpl(MenuRepository menuRepository, MenuMapper menuMapper) {
        this.menuRepository = menuRepository;
        this.menuMapper = menuMapper;
    }

    /**
     * Save a menu.
     *
     * @param menuDTO the entity to save.
     * @return the persisted entity.
     */
    @Override
    public MenuDTO save(MenuDTO menuDTO) {
        log.debug("Request to save Menu : {}", menuDTO);
        Menu menu = menuMapper.toEntity(menuDTO);
        menu = menuRepository.save(menu);
        return menuMapper.toDto(menu);
    }

    /**
     * Get all the menus.
     *
     * @return the list of entities.
     */
    @Override
    @Transactional(readOnly = true)
    public List<MenuDTO> findAll() {
        log.debug("Request to get all Menus");
        return menuRepository.findAll().stream()
            .map(menuMapper::toDto)
            .collect(Collectors.toCollection(LinkedList::new));
    }


    /**
     * Get one menu by id.
     *
     * @param id the id of the entity.
     * @return the entity.
     */
    @Override
    @Transactional(readOnly = true)
    public Optional<MenuDTO> findOne(Long id) {
        log.debug("Request to get Menu : {}", id);
        return menuRepository.findById(id)
            .map(menuMapper::toDto);
    }

    /**
     * Delete the menu by id.
     *
     * @param id the id of the entity.
     */
    @Override
    public void delete(Long id) {
        log.debug("Request to delete Menu : {}", id);
        menuRepository.deleteById(id);
    }

    @Override
    public ResultObj findByParams(int pageNum, int pageSize, String cname) {
        Pageable pageable = PageRequest.of(pageNum-1, pageSize);
        Page<Menu> menus = menuRepository.findByParams(cname,pageable);
        return ResultObj.back(200,menus);
    }

    @Override
    public ResultObj deleteTypeInfo(long id) {
        menuRepository.findById(id).ifPresent(menu -> {
            menu.setStatus("0");
            menuRepository.save(menu);
            List<Commodity> list = commodityRepository.findCommodityByType(menu.getType());
            for (Commodity c:list) {
                commodityService.deleteGoodsInfo(c.getId());
            }
        });
        return ResultObj.back(200,null);
    }

    @Override
    public ResultObj createTypeInfo(MenuDTO menuDTO) {
        Menu menu = menuMapper.toEntity(menuDTO);
        menu.setStatus("1");
        menuRepository.save(menu);
        return ResultObj.back(200,null);
    }

    @Override
    public ResultObj updateMenu(MenuDTO menuDTO) {
        if(menuRepository.findById(menuDTO.getId()).isPresent()){
            Menu menu = menuMapper.toEntity(menuDTO);
            menuRepository.save(menu);
            return ResultObj.back(200,null);
        }
        return ResultObj.back(201,null);

    }
}
